# -*- coding: utf-8 -*-
from google.appengine.ext import db
import md5
import urllib

class Blog(db.Model):
    """保存博客信息Database Model。
    
    在数据库中只有一条记录。
    
    Attributes:
        name: 布尔型，博客名称。
        title: 整数，博客主标题。
    """
    name=db.StringProperty(required=True)
    title=db.StringProperty(required=True)
    subtitle=db.StringProperty(required=True)
    bloger=db.StringProperty()
    email=db.StringProperty()
    theme=db.StringProperty(required=True)
    description=db.StringProperty(required=True)
    keywords=db.StringProperty(required=True)
    announcement=db.StringProperty()
    pageSize=db.IntegerProperty(required=True)
    
    def getVersion(self):
        return u"v1.0.0"

    def getSiteInfo(self):
        return u"""&#169; 2012 BigYuki.    """+self.getVersion()+"""   <a href="http://code.google.com/appengine/" 
            title="Semantic Personal Publishing Platform" 
            rel="generator">
            Proudly powered by Google App Engine. 
            </a> """
        
class Administrator(db.Model):
    """后台管理账号。
    
    储存后台管理账号。
    
    Attributes:
        email: Email，电子邮箱。
        password: String，密码(md5)加密。
    """
    username=db.StringProperty()
    password=db.StringProperty()

  
class Category(db.Model):
    name=db.StringProperty()
    def count(self):
        return Article.all().filter('category =', self).count()
    
class Archive(db.Model):
    year=db.IntegerProperty()
    month=db.IntegerProperty()
    def count(self):
        return Article.all().filter('archive =', self).count()

class Navigation(db.Model):
    name=db.StringProperty(required=True)
    link=db.StringProperty(required=True)
    weight=db.IntegerProperty(default=0)
    position=db.StringProperty(default='TOP')
    
class Article(db.Model):
    title=db.StringProperty(required=True)
    permalink =db.StringProperty(required=True)
    
    shortlink =db.StringProperty()
    
    content=db.TextProperty(required=True) 
    display=db.BooleanProperty(required=True)
    
    commentEnable=db.BooleanProperty(required=True)
    
    viewtimes=db.IntegerProperty(default=0)
    commenttimes=db.IntegerProperty(default=0)
    creadtedTime=db.DateTimeProperty(auto_now_add=True)
    modifiedTime=db.DateTimeProperty(auto_now=True)
       
    category=db.ReferenceProperty(Category,required=True)
    archive=db.ReferenceProperty(Archive,required=True)
    
    image=db.StringProperty()
    summary=db.TextProperty() 
    
    def getLocalCreatedTime(self):
        """获取当地区时的时间字符串 。
        Returns:
            一个字符串，把数据库对应0时区的时间转换为+8区的时间：
                2012-12-20 14:28。
        """
        from core.filters import TimeFilter 
        return TimeFilter.ToLocalTimeStr(self.creadtedTime)
    def next(self):
        return self.all().order('-creadtedTime').filter('creadtedTime <', self.creadtedTime).get()
    def previous(self):
        return self.all().order('creadtedTime').filter('creadtedTime >', self.creadtedTime).get()
    def getTags(self):
        tagList=[]
        tagRelationshipSet=TagRelationship.all().filter('article =', self)
        for relationItem in tagRelationshipSet:
            tagList.append(relationItem.tag)
        return tagList

class Tag(db.Model):
    name=db.StringProperty()
    
    def getArticles(self):
        articleList=[]
        tagRelationshipSet=TagRelationship.all().filter('tag =', self).order('-article')
        for relationItem in tagRelationshipSet:
            articleList.append(relationItem.article)
        return articleList
    def count(self):
        return len(self.getArticles())

class TagRelationship(db.Model):
    article=db.ReferenceProperty(Article)
    tag=db.ReferenceProperty(Tag)

class Comment(db.Model):
    article=db.ReferenceProperty(Article)
    isBloger=db.BooleanProperty(default=False)
    content=db.TextProperty()
    nickname=db.StringProperty()
    email=db.EmailProperty()
    website=db.StringProperty()
    time=db.DateTimeProperty(auto_now_add=True)
    ip=db.StringProperty()
    def getGravator(self):
        default = "/default.jpg"
        size = 100
        gravatar_url = "http://www.gravatar.com/avatar/" + md5.new(self.email.lower()).hexdigest() + "?"
        gravatar_url += urllib.urlencode({'d':default, 's':str(size)})
        return gravatar_url
    def getLocalCreatedTime(self):
        """获取当地区时的时间字符串 。
        
        Args:
            无
        Returns:
            一个字符串，把数据库对应0时区的时间转换为+8区的时间：
                2012-12-20 14:28。
        Raises:
           无
        """
        from core.filters import TimeFilter 
        return TimeFilter.ToLocalTimeStr(self.time)

